package org.core.tliaswebmanagement.controller;

import lombok.extern.slf4j.Slf4j;
import org.core.tliaswebmanagement.pojo.Result;
import org.core.tliaswebmanagement.utils.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@RestController
public class UploadController {

    /**
     * 文件上传到本地存储
     *
     * @param name
     * @param age
     * @param file
     * @return
     */
//    @PostMapping("/upload")
//    public Result upload(String name, Integer age, MultipartFile file) throws IOException {
//        log.info("接收参数：{}，{}，{}", name, age, file);
//
//        String originalFilename = file.getOriginalFilename();
//
//        file.transferTo(new File("D:\\20380\\javaproject\\filestore\\" + originalFilename));
//        return Result.success();
//    }

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

     @PostMapping("/upload")
    public  Result upload(MultipartFile file) throws Exception {
        log.info("文件上传：{}", file.getOriginalFilename());
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传OSS，返回url：{}", url);
        return Result.success(url);
    }
}
